# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from  . import  models

from django.shortcuts import render
from django.http import  HttpResponse

# Create your views here.


def index(request):
    cases = models.case.objects.all()

    return render(request, 'tools/index.html', {'cases': cases})


def case_page(request, case_id):
    case = models.case.objects.get(pk=case_id)
    return render(request, 'tools/case_page.html', {'case': case})


def edit_page(request, case_id):
    if str(case_id) == '0':
        return render(request, 'tools/edit_page.html')
    else:
        case = models.case.objects.get(pk=case_id)
        return render(request, 'tools/edit_page.html', {'case': case})


def create_case(request):

    case_id = request.POST.get('caseid', 'ID')
    case_name = request.POST.get('casename', 'NAME')
    pk_id = request.POST.get('id','0')
    if pk_id =='0':
        models.case.objects.create(case_id=case_id, case_name=case_name)
    else:
        case = models.case.objects.get(pk=pk_id)
        case.case_id=case_id
        case.case_name=case_name
        case.save()
    cases = models.case.objects.all()
    return render(request, 'tools/index.html', {'cases': cases})
